package com.tnovoselec.android.ui;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;


public class MyViewPager extends ViewPager {

    public interface OnPagerAdapterChangedListener {
	public void onPagerAdapterChanged(MyViewPager viewPager);
    }

    private Set<OnPagerAdapterChangedListener> pagerAdapterChangedListeners;
    private Set<OnPageChangeListener> pageChangedListeners;

    private boolean mTouchScroll;

    public MyViewPager(Context context) {
	this(context, null);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
	super(context, attrs);

	try {
	    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager);
	    mTouchScroll = a.getBoolean(R.styleable.MyViewPager_touchScroll, true);
	    a.recycle();
	} catch (Exception e) {
	}

	pagerAdapterChangedListeners = new HashSet<OnPagerAdapterChangedListener>();
	pageChangedListeners = new HashSet<OnPageChangeListener>();
	setOnPageChangeListener(pageChangeListener);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
	if (mTouchScroll) {
	    return super.onInterceptTouchEvent(event);
	} else {
	    return false;
	}
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
	if (mTouchScroll) {
	    return super.onTouchEvent(event);
	} else {
	    return false;
	}
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
	super.setAdapter(adapter);
	for (OnPagerAdapterChangedListener pagerAdapterChangedListener : pagerAdapterChangedListeners) {
	    if (pagerAdapterChangedListener != null) {
		pagerAdapterChangedListener.onPagerAdapterChanged(this);
	    }
	}
    }

    public boolean addOnPageChangeListener(OnPageChangeListener listener) {
	return this.pageChangedListeners.add(listener);
    }

    public boolean addOnPagerAdapterChangedListeners(OnPagerAdapterChangedListener listener) {
	return this.pagerAdapterChangedListeners.add(listener);
    }

    private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
	@Override
	public void onPageScrollStateChanged(int state) {
	    for (OnPageChangeListener listener : pageChangedListeners) {
		if (listener != null) {
		    listener.onPageScrollStateChanged(state);
		}
	    }
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
	    for (OnPageChangeListener listener : pageChangedListeners) {
		if (listener != null) {
		    listener.onPageScrolled(position, positionOffset, positionOffsetPixels);
		}
	    }
	}

	@Override
	public void onPageSelected(int position) {
	    for (OnPageChangeListener listener : pageChangedListeners) {
		if (listener != null) {
		    listener.onPageSelected(position);
		}
	    }
	}
    };

}
